#-*-coding:utf-8-*-
import sys
import os
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from asset_widget.progressBar_widget import MyProgressBar

class ProjectListWidget(QListWidget):
    path_list_Signal= Signal(list)#将路径列表传给主窗口

    def __init__(self):
        super(ProjectListWidget,self).__init__()


        #全局变量
        self.path_list=[]#当前列表中的真实路径


        #双击事件
        # self.doubleClicked.connect(self.__openFile)

        #添加右击菜单栏
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.__rightMenu)

        #开启拖拽
        self.setAcceptDrops(True)#可被拖入
        # self.setDragEnabled(True)#自身可拖出

        self.mainList=[] #当前project下文件夹名称

        #计时器
        self.timer=QBasicTimer()

        #进度条
        self.p=MyProgressBar()

    def __rightMenu(self):
        self.menu=QMenu()

        self.openAction=QAction(u'打开目录',self)
        self.openAction.triggered.connect(self.__openDir)
        self.menu.addAction(self.openAction)

        self.deleteAction=QAction(u'移除',self)
        self.deleteAction.triggered.connect(self.__deleteItem)
        self.menu.addAction(self.deleteAction)

        self.clearAction=QAction(u'清空列表',self)
        self.clearAction.triggered.connect(self.__clearItem)
        self.menu.addAction(self.clearAction)

        self.menu.exec_(QCursor.pos())

    def dragEnterEvent(self, event):
        '''
        \这里实现2个过程，一个是拖进去判定是否已存在，已存在则提示，未存在则加入全局list中保存，
        第二是拖进去完成后，发射信号给主窗口，信号内容为路径对数组列表
        :param event:
        :return:
        '''
        text=event.mimeData().text()
        text_list=text.split('\n')
        print(text_list)
        dirPath_list=[x[8:] for x in text_list]#拖入的文件夹路径
        # print(dirPath)
        # dirname = self.getFileNameByPath(dirPath)#当前拖入的文件夹名称

        for i in dirPath_list:
            if i not in self.path_list:
                self.path_list.append(i)

        self.refreshList(self.path_list)#重置列表显示



    def getFileNameByPath(self,path):
        '''
        根据给的路径，返回文件路径
        :param path:
        :return:
        '''
        dirpath,name=os.path.split(path)
        return name

    def refreshList(self,namelist):
        '''
        设置整个显示列表内容
        :param namelist:
        :return:
        '''

        self.clear()
        self.mainList=namelist
        self.addItems(namelist)

        self.path_list_Signal.emit(self.path_list)

    def addToList(self,name):
        '''
        将名称加入到self.mainList当中，然后更新
        :param name:
        :return:
        '''
        if name not in self.mainList:
            self.mainList.append(name)
            self.refreshList(self.mainList)

        else:
            QMessageBox.information(self,"提示","该文件已存在，请更新版本号再提交")





    def __deleteItem(self):
        filename = self.currentItem().text()
        if filename in self.path_list:
            self.path_list.remove(filename)
        self.refreshList(self.path_list)

    def __openDir(self):
        '''
        打开目录
        :return:
        '''
        filename=self.currentItem().text()
        dir=os.path.dirname(filename)
        if dir:
            os.startfile(dir)

    def __clearItem(self):
        self.clear()
        self.path_list=[]
        self.refreshList(self.path_list)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    F=ProjectListWidget()
    # nameList=['a','b','c','d']
    # F.refreshList(nameList)
    F.show()
    sys.exit(app.exec_())